package com.riki.riki_push;

import android.app.Activity;
import android.content.Context;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.riki.riki_push.util.CommonUtil;
import com.vivo.push.IPushActionListener;
import com.vivo.push.PushClient;

import java.lang.reflect.Method;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;


/**
 * RikiPushPlugin
 */
public class RikiPushVivPlugin implements FlutterPlugin, MethodCallHandler {
    /// The MethodChannel that will the communication between Flutter and native Android
    ///
    /// This local reference serves to register the plugin with the Flutter Engine and unregister it
    /// when the Flutter Engine is detached from the Activity
    private MethodChannel channel;
    private Context context;

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        context = flutterPluginBinding.getApplicationContext();
        channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "riki_push_vivo");
        channel.setMethodCallHandler(this);
    }

    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {

        try {
            if (call.method.equals("initVivo")) {
                initVivo(call, result);
            } else if (call.method.equals("getVivoRid")) {
                getVivoRid(call, result);
            } else if (call.method.equals("unInit")) {
                unInit(call, result);
            } else {
                result.notImplemented();
            }
        } catch (Exception e) {
            result.notImplemented();
        }


       /* try {
            Method method = this.getClass().getDeclaredMethod(call.method, MethodCall.class, Result.class);
            method.invoke(this, call, result);
        } catch (Exception e) {
            result.notImplemented();
        }*/

    }


    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        channel.setMethodCallHandler(null);
    }

    /**
     * 注册 vivo
     */
    private void initVivo(@NonNull MethodCall call, @NonNull final Result result) {
        // 初始化Push服务
        try {
            if (!PushClient.getInstance(context).isSupport()) return;
            PushClient.getInstance(context).initialize();
            PushClient.getInstance(context).turnOnPush(new IPushActionListener() {
                @Override
                public void onStateChanged(int i) {
                    if (i == 0) {
                        //开启成功
                        CommonUtil.runMainThreadReturn(result, 1);
//                        result.success(1);
                    } else CommonUtil.runMainThreadReturn(result, 0);
                }
            });
        } catch (Exception e) {
            CommonUtil.runMainThreadReturn(result, 0);
        }
    }


    /**
     * 获取vivo id
     */
    private void getVivoRid(@NonNull MethodCall call, @NonNull Result result) {
        // 初始化Push服务
        try {
            if (!PushClient.getInstance(context).isSupport()) {
                result.success(0);
                return;
            }
            String regId = PushClient.getInstance(context).getRegId();
            CommonUtil.runMainThreadReturn(result, regId);
        } catch (Exception e) {
            CommonUtil.runMainThreadReturn(result, 0);
        }
    }


    /**
     * 反初始化
     */
    private void unInit(@NonNull MethodCall call, @NonNull Result result) {
        try {
            PushClient.getInstance(context).turnOffPush(null);
            CommonUtil.runMainThreadReturn(result, 1);
        } catch (Exception e) {
            e.printStackTrace();
            CommonUtil.runMainThreadReturn(result, 0);
        }
    }


}
